home *** CD-ROM | disk | FTP | other *** search
/ Plug-In Power Pack for Netscape Communicator / Plug-In Power Pack for Netscape Communicator.iso / plugins / dataviews / dvtools / examples / programs / forms.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-08  |  12.4 KB  |  370 lines

  1. #ifndef lint
  2. static char SccsId[]= "@(#)forms.c    V1.18    3/13/95";
  3. #endif
  4. /*
  5. |    file -- forms.c
  6. |======================================================================
  7. |
  8. |    This example shows how to create a text entry "form".  The
  9. |    form items are entered using text entry input objects. The
  10. |    input object named "Item1" is the starting entry item in the
  11. |    form.  The user may move forwards through the form by using
  12. |    the <TAB> key and backwards using the <LINE FEED> key.
  13. |
  14. |    The program loads a view created in DV-Draw that has text
  15. |    entry input objects named "Item#".  Each of these objects
  16. |    is extracted from the drawing and it's bounding box is
  17. |    found and saved.  The bounding box information is used when
  18. |    drawing an outline to highlight the current text entry field
  19. |    as well as to focus keyboard input for the current input object.
  20. |
  21. |    Keyboard focus is done by changing the x,y position of the
  22. |    location object before passing it on to the handler,
  23. |    VUerHandleLocEvent.  The position is changed to be the center
  24. |    of the current text entry field. Therefore, regardless of the
  25. |    cursor position, keyboard entry will appear in the currently
  26. |    highlighted text entry field.
  27. |
  28. |    The left mouse button is used to position the carat within the
  29. |    current text entry input object.  The left mouse button event
  30. |    is only used if position of the cursor is within the currently
  31. |    highlighted text entry input object.
  32. |
  33. |    To exit the program, use the right mouse button or select the
  34. |    <ESC> key.
  35. |
  36. |=======================================================================
  37. */
  38. #include <windows.h>
  39. /*
  40.  *  DV-Tools header files
  41.  */
  42. #include "std.h"                /* <stdio.h> etc., scalar & macro definitions */
  43. #include "dvstd.h"              /* public types & constants */
  44. #include "dvtools.h"            /* constants used by T routines */
  45. #include "dvGR.h"               /* constants used by window mgt & GR routines */
  46. #include "VOstd.h"              /* constants used by VO & VOob routines */
  47. #include "Tfundecl.h"           /* T routines (screens, drawports & views) */
  48. #include "VOfundecl.h"          /* VO routines (objects) */
  49. #include "VUerfundecl.h"        /* VUer routines (event handling routines) */
  50.  
  51. /* Constants */
  52. #define  DVPATH            (char *)NULL
  53. #define  DISPFORMS_STB     (char *)NULL
  54. #define  DVDEVICE          (char *)NULL
  55. #define  DVCOLORTABLE      (char *)NULL
  56. #define  VIEW_NAME         "forms.v"
  57. #define  SCREEN_VIEWPORT   (RECTANGLE *)NULL
  58. #define  NUM_OF_TEXT_AREAS 8
  59. #define  XK_BackSpace      0x08
  60. #define  XK_Tab            0x09
  61. #define  XK_Linefeed       0x0A
  62. #define  XK_Escape         0x1B
  63.  
  64. /* Whole world rectangle, (-16384, -16384) to (16383, 16383)*/
  65. RECTANGLE whole_world = {XMIN, YMIN, XMAX, YMAX};
  66.  
  67. /* Bounding areas around text entry input objects and the center
  68.    point of these areas */
  69. RECTANGLE TextArea[NUM_OF_TEXT_AREAS];
  70. DV_POINT TextAreaCenter[NUM_OF_TEXT_AREAS];
  71.  
  72.  
  73. /* Functions defined in forms.c */
  74. void Outline V_P_((DRAWPORT dp, OBJECT obj, int index));
  75.  
  76.  
  77. /*
  78.  *   MAIN PROGRAM
  79.  */
  80. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, 
  81.                      LPSTR lpCmdLine, int nCmdShow )
  82. {
  83.   INT argc = 0;
  84.   CHAR **argv;
  85.   /*
  86.    *  program arguments
  87.    *    argv[1] - display device name (default is to use DVDEVICE)
  88.    */
  89.  
  90.   /* Define & initialize device name and view filename */
  91.   char *device_name = DVDEVICE; /* default device name */
  92.   char *view_name = VIEW_NAME;  /* default view name */
  93.  
  94.   /* Define display variables */
  95.   OBJECT screen;                /* display device, the window */
  96.   DRAWPORT drawport;            /* how & where to display picture, picture frame */
  97.   VIEW view;                    /* picture representation of the view file */
  98.  
  99.   /* Control loop variables */
  100.   OBJECT location;              /* the event representation */
  101.  
  102.   /* Other variables */
  103.   OBJECT drawing,               /* graphical representation of screen */
  104.     obj,                        /* object from drawing */
  105.     outline,                    /* highlight object - rectangle */
  106.     xform;                      /* transformation object */
  107.   int current,                  /* tag for current text entry input object */
  108.       i,                        /* counter */
  109.       keysym;                   /* selected key symbol */
  110.   int Quit = NO;                /* flag to exit the program */
  111.   DV_POINT scs;                 /* screen coordinate point */
  112.   DV_POINT *wpt;                /* world coordinate point of location object */
  113.   WINEVENT *we;                 /* structure storing details of event */
  114.   RECTANGLE dummy;              /* offset which is not used */
  115.   char obj_name[100];           /* part of names of objects in drawing */
  116.  
  117.   /*--------------------
  118.    *   Initialization
  119.    *
  120.    *   TInit:  perform the initialization of DV-Tools
  121.    *           TInit reads your configuration file and any
  122.    *           environment variables or logical names set.
  123.    */
  124.   make_argv(&argc,&argv,GetCommandLine());
  125.   TInit( DVPATH, DISPFORMS_STB );
  126.  
  127.   /*
  128.    *   TscOpenSet: open a device as a screen object using
  129.    *               specified attributes
  130.    *   TscErase:   erase the entire screen in the default
  131.    *               background color
  132.    *
  133.    *   Set exposure block to YES to insure the window
  134.    *   is ready for drawing when TdpDraw is called.
  135.    */
  136.   if (argc > 1)
  137.     device_name = argv[1];
  138.   screen = TscOpenSet (device_name, DVCOLORTABLE,
  139.                        V_X_EXPOSURE_BLOCK, YES,
  140.                        V_ACTIVE_CURSOR, V_END_OF_LIST);
  141.   if (!screen)
  142.     {
  143.       printf ("Must specify device on command line or");
  144.       printf (" in DataViews configuration file.\n");
  145.       S_EXIT (EXIT_ERR);
  146.     }
  147.   TscErase (screen);
  148.  
  149.   /*
  150.    *   VOscWinEventMask:  sets the screen's window event mask
  151.    */
  152.   VOscWinEventMask ((ULONG) V_KEYPRESS | V_KEYRELEASE |
  153.                             V_BUTTONPRESS | V_BUTTONRELEASE |
  154.                             V_RESIZE | V_EXPOSE,
  155.                     (ULONG) 0);
  156.  
  157.   /*
  158.    *   TviLoad:   Load a view in from a file
  159.    *   TdpCreate: Create a DV-Tools window, a drawport.
  160.    *              The drawport is attached to the screen object
  161.    *              specified while view specifies the view to be
  162.    *              displayed on the screen.
  163.    */
  164.   view = TviLoad (view_name);
  165.   if (!view)
  166.     {
  167.       printf ("Could not load view from file ");
  168.       printf ("%s.\n", view_name);
  169.       S_EXIT (EXIT_ERR);
  170.     }
  171.   drawport = TdpCreate (screen, view, SCREEN_VIEWPORT, &whole_world);
  172.  
  173.   /*
  174.    *   TviGetDrawing:     Gets a view's drawing object
  175.    *   TdpGetXform:      Get drawport transformation
  176.    *   TdpDraw:          Draw contents of drawport
  177.    */
  178.   drawing = TviGetDrawing (view);
  179.   xform = TdpGetXform (drawport, DR_TO_SCREEN);
  180.   TdpDraw (drawport);
  181.  
  182.   /*
  183.    *   TdrGetNamedObject: Gets a named object from a
  184.    *                      drawing.
  185.    *   VOobBox:          Gets an object's bounding box
  186.    *
  187.    *   Get the text input objects and store the bounding
  188.    *   box for each of these text input objects.
  189.    */
  190.   for (i = 0; i < NUM_OF_TEXT_AREAS; i++)
  191.     {
  192.       sprintf (obj_name, "Item%d", i + 1);
  193.       obj = TdrGetNamedObject (drawing, obj_name);
  194.       VOobBox (obj, &TextArea[i], &dummy);
  195.       TextAreaCenter[i].x = (TextArea[i].ll.x + TextArea[i].ur.x) / 2;
  196.       TextAreaCenter[i].y = (TextArea[i].ll.y + TextArea[i].ur.y) / 2;
  197.     }
  198.  
  199.   /*
  200.    *   Get the named object "outline" which will be used to
  201.    *   highlight the current text entry input object.
  202.    */
  203.   outline = TdrGetNamedObject (drawing, "outline");
  204.   current = 0;
  205.   Outline (drawport, outline, current);
  206.  
  207.   FOREVER
  208.   {
  209.     /*
  210.      *  VOloWinEventPoll:  Poll for the next window event.
  211.      *                     The polling mode used is V_WAIT.
  212.      *                     Therefore, VOloWinEventPoll does not
  213.      *                     return until a masked event is
  214.      *                     generated.  V_WAIT always produces
  215.      *                     a valid location object.
  216.      */
  217.     location = VOloWinEventPoll (V_WAIT);
  218.  
  219.     /*
  220.      *  VOloType:  returns the type of event.  These types
  221.      *             match event types specified in VOscWinEventMask.
  222.      */
  223.     switch (VOloType (location))
  224.       {
  225.  
  226.       case V_RESIZE:
  227.         /*
  228.          *  The window size has been changed.
  229.          *  TscReset:  Resets all screen drawports after
  230.          *             window resizing
  231.          */
  232.         TscReset (screen);
  233.         break;
  234.  
  235.       case V_EXPOSE:
  236.         /*
  237.          *  VOloRegion:  Returns a rectangle representing the
  238.          *               exposed region on the screen.
  239.          *  TscRedraw:   After erasing, redraws all the drawports
  240.          *               in the screen.
  241.          *  A portion of the window has been exposed and needs
  242.          *  to be redrawn.
  243.          */
  244.         TscRedraw (screen, VOloRegion (location));
  245.         break;
  246.  
  247.       case V_KEYPRESS:
  248.         /*
  249.          *  Check key selected.
  250.          *  VOloKeySym:  Returns the key symbol value of the
  251.          *               location object
  252.          */
  253.         keysym =  (int)VOloKeySym (location);
  254.         switch (keysym)
  255.           {
  256.           case XK_Escape:
  257.             Quit = YES;
  258.             break;
  259.  
  260.           case XK_Tab:
  261.             if (current < NUM_OF_TEXT_AREAS - 1)
  262.               current++;
  263.             else
  264.               current = 0;
  265.             Outline (drawport, outline, current);
  266.             break;
  267.  
  268.           case XK_Linefeed:
  269.             if (current > 0)
  270.               current--;
  271.             else
  272.               current = NUM_OF_TEXT_AREAS - 1;
  273.             Outline (drawport, outline, current);
  274.             break;
  275.  
  276.             /*
  277. *  VOloWinEventGet:    Returns the window event structure of
  278. *                 the location object
  279. *  VOxfPoint:         Transforms point according to the
  280. *                 transform object
  281. *  TloWinEventSetup:   Sets up location object WINEVENT values
  282. *  VUerHandleLocEvent: Service event
  283. *
  284. *  Modify the location objects window event structure.
  285. *  Change the location of the cursor to represent a
  286. *  position in the center of the current text entry field.
  287. */
  288.           default:
  289.             we = (WINEVENT *) VOloWinEventGet (location);
  290.             scs = TextAreaCenter[current];
  291.             VOxfPoint (xform, &scs);
  292.             we->loc.x = scs.x;
  293.             we->loc.y = scs.y;
  294.             TloWinEventSetup (location, we, screen, drawport);
  295.             VUerHandleLocEvent (location);
  296.             break;
  297.           }
  298.         break;
  299.  
  300.       case V_BUTTONPRESS:
  301.         /*
  302.          *  VOloButton:  Returns the button that was pressed
  303.          *
  304.          *  The right mouse button exits the program.
  305.          */
  306.         switch (VOloButton (location))
  307.           {
  308.           case 1:
  309.             wpt = VOloWcpGet (location);
  310.             if (TextArea[current].ll.x <= wpt->x &&
  311.                 TextArea[current].ll.y <= wpt->y &&
  312.                 wpt->x <= TextArea[current].ur.x &&
  313.                 wpt->y <= TextArea[current].ur.y)
  314.               VUerHandleLocEvent (location);
  315.             break;
  316.  
  317.           case 3:
  318.             Quit = YES;
  319.             break;
  320.           }
  321.         break;
  322.  
  323.       default:
  324.         break;
  325.       }
  326.  
  327.     /* exit the program */
  328.     if (Quit == YES)
  329.       break;
  330.   }
  331.  
  332.   /*--------------------
  333.    *   Termination
  334.    *
  335.    *   TdpDestroy:   Destroy the drawport,
  336.    *   TviDestroy:   Destroy the view, freeing the allocated memory
  337.    *   TscCloseCurrentScreen:  Close the current display screen
  338.    *   TTerminate:   Perform the clean-up for DV-Tools
  339.    */
  340.   TdpDestroy (drawport);
  341.   TviDestroy (view);
  342.   TscCloseCurrentScreen ();
  343.   TTerminate ();
  344.   return EXIT_OK;
  345. }
  346.  
  347. /*----------
  348.  *   Outline -- highlight the current text entry input object
  349.  *        by drawing a box around it.  The outline object
  350.  *        is moved from the previous highlighted text
  351.  *        input object to the new current text input object.
  352.  *        The bounding box is extended to be outside the
  353.  *        object.
  354.  */
  355. void 
  356. Outline (dp, obj, index)
  357.      DRAWPORT dp;
  358.      OBJECT obj;
  359.      int index;
  360. {
  361.   TdpEraseObject (dp, obj);
  362.   VOptMove (VOobPtGet (obj, 1), DV_ABSOLUTE,
  363.                (TextArea[index].ll.x - 200),
  364.                (TextArea[index].ll.y - 200));
  365.   VOptMove (VOobPtGet (obj, 2), DV_ABSOLUTE,
  366.                 (TextArea[index].ur.x + 200),
  367.                 (TextArea[index].ur.y + 200));
  368.   TdpDrawObject (dp, obj);
  369. }
  370.